﻿using System;
using System.Xml.Serialization;
using System.Web.Script.Serialization;

using Fuse8.DomainFramework.Domain;
using Fuse8.DomainFramework.BusinessService;
using Fuse8.DomainFramework.CacheService;

namespace Fuse8.ContentManagementFramework.Domain
{
    [Serializable]
    public partial class CmsEntityMeta : Int32EntityObject
    {
        #region Linked fields

        private CmsEntityMetaFieldLink _entityMetaFieldLink;

        #endregion

        #region Properties

        public virtual int EntityMetaFieldLinkId { get; set; }
        
        public virtual int EntityId { get; set; }        

        public virtual string EntityMetaValue { get; set; }

        #endregion

        #region Linked properties
        
        [XmlIgnore]
        [ScriptIgnore]
        public CmsEntityMetaFieldLink EntityMetaFieldLink
        {
            get
            {
                if (_entityMetaFieldLink == null)
                {
                    var businessFacade = new BusinessServiceFacade<CmsEntityMetaFieldLink, int>();
                    if (businessFacade != null)
                    {
                        _entityMetaFieldLink = businessFacade.FindOne(EntityMetaFieldLinkId);
                    }
                }

                return _entityMetaFieldLink;
            }
            set
            {
                EntityMetaFieldLinkId = value != null ? value.Id : 0;

                if (_entityMetaFieldLink != null)
                {
                    _entityMetaFieldLink = _entityMetaFieldLink.Id == EntityMetaFieldLinkId ? value : null;
                }
                else
                {
                    _entityMetaFieldLink = value;
                }
            }
        }

        #endregion

        #region Constructor

        public CmsEntityMeta()
        { }

        #endregion

        #region EntityObject override

        public override string TypeParameter
        {
            get
            {
                return "EntityMeta";
            }
        }

        #endregion
    }
}
